/**
 * Copyright (c) 2012 Nokia Corporation.
 */

package com.nokia.example.simplepurchase.uicomponents;

import java.io.IOException;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Contains the application buttons. Will paint the buttons and provide the
 * clicked events.
 */
public class Buttons {
	// Constants
	private static final int MARGIN = 6;
	private static final int BUTTON_WIDTH = 214;
	private static final int BUTTON_HEIGHT = 37;
	
	// Button IDs
	public static final int POSITIVE_ACTION = 0;
	public static final int NEGATIVE_ACTION = 1;
	public static final int EXIT = 2;
	
	// Members
	private final Font _font;
	private Listener _listener = null;
	private Image _buttonPositiveNormalImage = null;
	private Image _buttonPositivePressedImage = null;
	private Image _buttonNegativeNormalImage = null;
	private Image _buttonNegativePressedImage = null;
	private Image _exitButtonNormalImage = null;
	private Image _exitButtonPressedImage = null;
	private int _buttonPressed = -1;
	private String[] _buttonText = new String[2];
	private boolean[] _buttonVisible = new boolean[3];
	private int _width = 0;
	private int _height = 0;
	
	/**
	 * Constructor.
	 */
	public Buttons(Listener listener, int width, int height) {
		_font = Font.getDefaultFont();		
		_listener = listener;
		_width = width;
		_height = height;
		
		try {
			_buttonPositiveNormalImage = Image.createImage("/graphics/ft/button-blue-normal.png");
			_buttonPositivePressedImage = Image.createImage("/graphics/ft/button-blue-pressed.png");
			_buttonNegativeNormalImage = Image.createImage("/graphics/ft/button-red-normal.png");
			_buttonNegativePressedImage = Image.createImage("/graphics/ft/button-red-pressed.png");
			_exitButtonNormalImage = Image.createImage("/graphics/ft/exit-button-normal-30.png");
			_exitButtonPressedImage = Image.createImage("/graphics/ft/exit-button-pressed-30.png");
		}
		catch (IOException ex) {
			System.out.println("Failed to load button image!");
		}
		
		_buttonVisible[2] = true;
	}
	
	/**
	 * Sets the texts and the visibility of the buttons.
	 * @param text1
	 * @param visible1
	 * @param text2
	 * @param visible2
	 */
	public void setButtons(String text1, boolean visible1,
						   String text2, boolean visible2)
	{
		_buttonText[0] = text1;
		_buttonText[1] = text2;
		_buttonVisible[0] = visible1;
		_buttonVisible[1] = visible2;
	}

	/**
	 * Paints the buttons.
	 * @param graphics
	 */
	public void paint(Graphics graphics) {
		final int buttonX = (_width - BUTTON_WIDTH) / 2;
		final int button1y = _height - 30 - MARGIN * 3 - BUTTON_HEIGHT * 2;
		final int button2y = button1y + MARGIN + BUTTON_HEIGHT;
		final int textY = (BUTTON_HEIGHT - _font.getHeight()) / 2;
		
		Image imageToDraw = null;
		
		if (_buttonText[0] != null && _buttonVisible[0]) {
			if (_buttonPressed == POSITIVE_ACTION) {
				imageToDraw = _buttonPositivePressedImage;
			}
			else {
				imageToDraw = _buttonPositiveNormalImage;
			}
			
			graphics.drawImage(imageToDraw, buttonX, button1y,
					Graphics.TOP | Graphics.LEFT);
			graphics.setColor(0xffffffff);
			graphics.drawString(_buttonText[0],
					buttonX + (BUTTON_WIDTH - _font.stringWidth(_buttonText[0])) / 2,
					button1y + textY,
					Graphics.TOP | Graphics.LEFT);
		}

		if (_buttonText[1] != null && _buttonVisible[1]) {
			if (_buttonPressed == NEGATIVE_ACTION) {
				imageToDraw = _buttonNegativePressedImage;
			}
			else {
				imageToDraw = _buttonNegativeNormalImage;
			}
			
			graphics.drawImage(imageToDraw, buttonX, button2y,
					Graphics.TOP | Graphics.LEFT);
			graphics.setColor(0xffffffff);
			graphics.drawString(_buttonText[1],
					buttonX + (BUTTON_WIDTH - _font.stringWidth(_buttonText[1])) / 2,
					button2y + textY,
					Graphics.TOP | Graphics.LEFT);
		}
		
		if (_buttonVisible[EXIT]) {
			if (_buttonPressed == EXIT) {
				imageToDraw = _exitButtonPressedImage;
			}
			else {
				imageToDraw = _exitButtonNormalImage;
			}

			graphics.drawImage(imageToDraw, _width - 30, _height - 30,
					Graphics.TOP | Graphics.LEFT);
		}
	}
	
	/**
	 * Handles pointer pressed events.
	 * @param x
	 * @param y
	 */
	public void onPointerPressed(int x, int y) {
		_buttonPressed = buttonAt(x, y);
	}
	
	/**
	 * Handles pointer released events.
	 * @param x
	 * @param y
	 */
	public void onPointerReleased(int x, int y) {
		final int button = buttonAt(x, y);

		if (button > -1 && button == _buttonPressed) {
			_listener.onButtonClicked(button);
		}
		
		_buttonPressed = -1;
	}
	
	/**
	 * Resolves the button at the given coordinates.
	 * @param x
	 * @param y
	 * @return The ID of the button or -1 if no button in the given coordinates.
	 */
	private int buttonAt(int x, int y) {
		final int buttonX = (_width - BUTTON_WIDTH) / 2;
		final int button1y = _height - 30 - MARGIN * 3 - BUTTON_HEIGHT * 2;
		final int button2y = button1y + MARGIN + BUTTON_HEIGHT;
		
		if (x > buttonX && x < buttonX + BUTTON_WIDTH) {
			if (y > button1y && y < button1y + BUTTON_HEIGHT
					&& _buttonVisible[POSITIVE_ACTION])
			{
				return POSITIVE_ACTION;
			}
			else if (y > button2y && y < button2y + BUTTON_HEIGHT
					&& _buttonVisible[NEGATIVE_ACTION]) {
				return NEGATIVE_ACTION;
			}
		}
		
		if (x > _width - 36 && y > _height - 36 && _buttonVisible[EXIT]) {
			return EXIT;
		}
		
		return -1;
	}
	
	/**
	 * Listener interface for button clicked events.
	 */
	public interface Listener {
		void onButtonClicked(int id);
	}
}
